<?php

class Imei {

    protected $main_table = 'data_imei';
    protected $main_id = 'imei_id';
    protected $main_code = 'imei_code';
    protected $main_table2 = 'data_mobile_log';
    protected $main_id2 = 'log_id';
    protected $db = null;

    public function __construct($db) {
	$this->db = $db;
    }

    public function __destruct() {
	unset($this->db);
    }

    public function get_table_name() {
	return $this->main_table;
    }

    /**
     * 	insert news
     * 	@param array $data 
     * 	@return int affected count
     */
    public function insert_data($data) {
	if (!is_array($data))
	    return false;
	$rows = $this->db->insert($this->main_table, $data);
	$insert_id = $this->db->getInsertId();
	return $insert_id;
    }

    public function insert_log_data($data) {
	if (!is_array($data))
	    return false;
	$rows = $this->db->insert($this->main_table2, $data);
	$insert_id = $this->db->getInsertId();
	return $insert_id;
    }
    /**
     * 	update news
     * 	@param array $data 
     * 	@param string $where 
     * 	@return int affected count
     */
    public function update_data($data, $where) {
	if (!is_array($data))
	    return false;
	$result = $this->db->update($this->main_table, $data, $where);
	return $result;
    }

    
     public function update_log_data($data, $where) {
	if (!is_array($data))
	    return false;
	$result = $this->db->update($this->main_table2, $data, $where);
	return $result;
    }
    /**
     * 	delete news
     * 	@param string $where 
     * 	@return int affected count
     */
    public function del_data($where) {
	$result = $this->db->delete($this->main_table, $where);
	return $result;
    }

    /**
     * 	get news info
     * 	@param int $id 
     * 	@param array/string $field
     * 	@return array one row data
     */
    public function get_data_row($sn, $field = '*') {
	if (!intval($sn))
	    return false;
	$sql = "SELECT " . $field . " FROM " . $this->main_table . " WHERE imei_channel_sn ='$sn' ";
	$data = $this->db->getRow($sql);
	return $data;
    }
	
	/**
	* 	get news array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_imei_array($where,$order='imei_id DESC',$limit='10',$field){
		$sql = "SELECT ".$field." FROM ".$this->main_table." left join data_channel on imei_channel_sn =  channel_sn WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}  
    
     public function get_last_log($imei) {
	$sql = "SELECT " . $this->main_id2 . " FROM " . $this->main_table2 . " WHERE log_imei='".$imei."' order by " . $this->main_id2 . " desc limit 1 ";
	$data = $this->db->getOne($sql);
	return $data;
    }
    

    public function get_imei_row($code, $field = '*') {
	if (!trim($code))
	    return false;
	$sql = "SELECT " . $field . " FROM " . $this->main_table . " WHERE " . $this->main_code . "='" . trim($code) . "' ";
	$data = $this->db->getRow($sql);
	return $data;
    }

    //打开软件
    public function open_soft($imei, $channel,$brand='',$model='',$sdk='') {
		if ($imei) {
			$row = $this->get_imei_row($imei);
			if ($row) {	//存在,更新
			$update = array('imei_login_count' => "imei_login_count+1",'imei_last_open' => date("Y-m-d H:i:s"));
			if($model && !$row['imei_phone_model']){
				$update['imei_phone_brand'] = $brand;
				$update['imei_phone_model'] = $model;
				$update['imei_phone_sdk'] = $sdk;
			}
			$rs = $this->update_data($update, $this->main_code . "='" . trim($imei) . "' ");
			} else {
			$insert = array(//新记录
				'imei_phone_brand' => $brand,
				'imei_phone_model' => $model,
				'imei_phone_sdk' => $sdk,
				'imei_code' => $imei,
				'imei_channel_sn' => trim($channel),
				'imei_first_open' => date("Y-m-d H:i:s"),
				'imei_login_count' => "1",
				'imei_last_open' => date("Y-m-d H:i:s")
			);
			$rs = $this->insert_data($insert);
			}
			
		  
			
		} else {
			//软件 imei码被屏蔽
			$rs = false;
		}
	    return $rs;
    }

	
	 //打开软件
    public function open_ios_soft($imei,$model,$version,$sdk,$channel) {
		if ($imei) {
			$row = $this->get_imei_row($imei);
			if ($row) {	//存在,更新
				$update = array('imei_login_count' => "imei_login_count+1",'imei_last_open' => date("Y-m-d H:i:s"));			
				$this->update_data($update, $this->main_code . "='" . trim($imei) . "' ");
			}else{
				$insert = array(//新记录	
					'imei_phone_brand' => 'apple',		
					'imei_phone_model' => $model,	
					'imei_phone_sdk' => $sdk,		
					'imei_code' => $imei,	
					'imei_channel_sn' => $channel,	    
					'imei_first_open' => date("Y-m-d H:i:s"),
					'imei_login_count' => "1",
					'imei_last_open' => date("Y-m-d H:i:s"),
					'imei_type' => '1'
				);
				$this->insert_data($insert);
			}	
			
			$sql = "select * from data_channel where channel_sn ='0000001' and  channel_version > " . $version;
			$channel_row = $this->db->getRow($sql);	
			if ($channel_row) {
				$output['status'] = '1';
				$output['url'] = SITE_ADDR . trim($channel_row['channel_apk_url'], "/");
				$output['version'] = $channel_row['channel_version'];
				$output['title'] = '版本更新提示';
				$output['info'] = $channel_row['channel_update_info'];
				return $output;  			
			}	
		}else{
			$output['status'] = '0';
			return $output;  			
		}
		
		
    }





    //关闭软件
    public function close_soft($imei){
		$row = $this->get_imei_row($imei,'imei_code');
		if($row){
		   $update = array('imei_last_close' => date("Y-m-d H:i:s")	);      
			$this->update_data($update, $this->main_code . "='" . trim($imei) . "' "); 
		}	
    }
    
    //卸载软件,未能实现
    public function uninstall_soft($imei){
	$row = $this->get_imei_row($imei,'imei_code');
	if($row){
	   $update = array('imei_uninstall_time' => date("Y-m-d H:i:s")	);
	    $rs = $this->update_data($update, $this->main_code . "='" . trim($imei) . "' "); 
	}
    }

    //后退软件
    public function back_soft($imei){
        $row = $this->get_imei_row($imei,'imei_code');
        if($row){
            $update = array('imei_back_count' => "imei_back_count+1",'imei_back_time' => date("Y-m-d H:i:s"));
            //$this->db->debug(1,1);
            $rs = $this->update_data($update, $this->main_code . "='" . trim($imei) . "' ");
        }
        return $rs;
    }

    //用户注册,更新imei信息
    public function user_reg($imei,$uid,$user) {
	$row = $this->get_imei_row($imei);
	if($row && $uid && $user){
	    if(!$row['imei_uid']){						//没注册信息
		$update = array(
		    'imer_reg_count' => "imer_reg_count+1",
		    'imei_uid' => $uid,
		    'imei_user' => $user,
		    'imei_reg_time' => date("Y-m-d H:i:s"),
		);
		$rs = $this->update_data($update, $this->main_code . "='" . trim($imei) . "' ");
	    }
	}
    }

    //用户手机激活,更新imei信息
    public function user_active($imei,$phone) {
	$row = $this->get_imei_row($imei,'imei_reg_phone');
	if($row && $phone){
	    if(!$row['imei_reg_phone']){
            $update = array(
                'imei_reg_phone' => $phone,			//手机号
                'imei_active_time' => date("Y-m-d H:i:s"),
            );
            $rs = $this->update_data($update, $this->main_code . "='" . trim($imei) . "' ");
	    }else{
            $rs = true;
        }
	}else{
        $rs = false;
    }
        return $rs;
    }
	
	//掌友安装包信息中的安装数
	public function get_index_install_num($sn){		
		$sql="SELECT count(imei_code) as install_num,imei_channel_sn as channel_sn FROM " . $this->main_table . " WHERE imei_channel_sn IN (".$sn.") GROUP BY imei_channel_sn ";
		$row = $this->db->getAll($sql);
		return $row;
	}
	
	//掌友安装包信息中的注册数
	public function get_index_regester_num($sn){
		$sql="SELECT count(imei_uid) as regester_num,imei_channel_sn as channel_sn FROM  " . $this->main_table . " WHERE imei_channel_sn IN (".$sn.") and imei_uid != '0' GROUP BY imei_channel_sn ";		
		$row = $this->db->getAll($sql);
		return $row;
	}
    
  	//各渠道的安装数
	public function get_install_count($where){
		$sql = "SELECT count(*) as num,imei_channel_sn FROM `data_imei` WHERE $where GROUP BY imei_channel_sn";
		$data = $this->db->getAll($sql);
		return $data;	
	}
	
	//注册用户数
	public function get_reg_count($where){
		$sql = "SELECT count(*) as num,imei_channel_sn FROM `data_imei`  WHERE $where and imei_uid>'0' GROUP BY imei_channel_sn";
		$data = $this->db->getAll($sql);		
		return $data;	
	
	}
	
	//用户激活数
	public function get_active_count($where){
		$sql = "SELECT count(*) as num,imei_channel_sn FROM `data_imei` WHERE $where and imei_reg_phone>'' GROUP BY imei_channel_sn";
		$data = $this->db->getAll($sql);
		return $data;	
	
	}
	
    
}

?>